package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.modules.phy.dto.DateDto;
import com.cookor.poi.modules.phy.entity.FSchedue;
import com.cookor.poi.modules.phy.service.FSchedueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping({"/myTest"})
public class MyTestController {

    @Autowired
    private FSchedueService fSchedueService;
    @RequestMapping("/test")
    public List<DateDto> test(){
        FSchedue query = new FSchedue();
        query.setTenantId(5);
        List<FSchedue> list = this.fSchedueService.findList(query);
        List<DateDto> dateList = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        int hour = c.get(11);
        int i = 0;
        boolean flag = true;
        boolean breakFlag = false;
        int week = c.get(Calendar.DAY_OF_WEEK);
        while (flag) {
            DateDto futureDate = DateUtils.getFutureDate(i);
            for (FSchedue fSchedue : list) {
                Integer year = futureDate.getYear();
                String monthStr = futureDate.getDate().split("-")[0];
                String dayStr = futureDate.getDate().split("-")[1];
                Integer month = Integer.valueOf(Integer.parseInt((monthStr.indexOf("0") == 0) ? monthStr.substring(1) : monthStr));
                Integer day = Integer.valueOf(Integer.parseInt((dayStr.indexOf("0") == 0) ? dayStr.substring(1) : dayStr));
                // 特殊假日的处理方式
//                if (day != 6 && day != 7) {
//                    continue;
//                }
//                if (fSchedue.getScheduleYear().equals(year) && fSchedue.getScheduleMonth().equals(month) && fSchedue
//                        .getScheduleDay().equals(day)) {
//                    dateList.add(futureDate);
//                    //flag = false;
//                    break;
//                }
//            }
//            if (hour > 7) {
//                if (dateList.size() > 1) {
//                    flag = false;
//                }
//            } else if (dateList.size() > 0) {
//                flag = false;
//            }
//            i++;
                // 平日的处理方式
                if (fSchedue.getScheduleYear().equals(year) && fSchedue.getScheduleMonth().equals(month) && fSchedue
                        .getScheduleDay().equals(day)) {
                    if ((fSchedue.getTotalA() == null || fSchedue.getTotalA().equals(Integer.valueOf(0))) && (fSchedue
                            .getTotalB() == null || fSchedue.getTotalB().equals(Integer.valueOf(0))) && (fSchedue
                            .getTotalC() == null || fSchedue.getTotalC().equals(Integer.valueOf(0))) && (fSchedue
                            .getTotalD() == null || fSchedue.getTotalD().equals(Integer.valueOf(0))) && (fSchedue
                            .getTotalE() == null || fSchedue.getTotalE().equals(Integer.valueOf(0)))) {
                        break;
                    }
                    if (dateList.size() == 0 && i != 0) {
                        breakFlag = true;
                    }
                    dateList.add(futureDate);
                    break;
                }
            }
            if (hour > 7) {
//                if ((dateList.size() == 1 && breakFlag) || dateList.size() > 1) {
//                    flag = false;
//                }
                if(breakFlag){ // 当日没有号源
                    if (dateList.size() > 0) { // 那就往后找一天
                        flag = false;
                    }
                }else{
                    if (dateList.size() > 1) { // 否则就往后找两天
                        flag = false;
                    }
                }
//                if (dateList.size() > 1) {
//                    flag = false;
//                }
            } else if (dateList.size() > 0) {
                flag = false;
            }
            i++;
//            if (week == 6 && i > 3) { //周五往后推三天
//                flag = false;
//            }
//            if (week == 7 && i > 2) { //周六往后推二天
//                flag = false;
//            }
//            if (week == 1 && i > 1) { //周日往后推一天
//                flag = false;
//            }
//            if ((week != 6 && week != 7 && week != 1) && i > 1) { // 平常就当日
//                flag = false;
//            }

        }
        return dateList;
    }

}
